package com.yonyou.cyxdms.basedata.service.service.questionaire;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.bean.BeanMapperUtil;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyxdms.basedata.service.dto.questionaire.AnswerDTO;
import com.yonyou.cyxdms.basedata.service.dto.questionaire.AnswerGroupDTO;
import com.yonyou.cyxdms.basedata.service.dto.questionaire.QueryAnswerDTO;
import com.yonyou.cyxdms.basedata.service.entity.questionaire.AnswerGroupPO;
import com.yonyou.cyxdms.basedata.service.entity.questionaire.AnswerPO;
import com.yonyou.cyxdms.basedata.service.repository.questionaire.AnswerGroupMapper;
import com.yonyou.cyxdms.basedata.service.repository.questionaire.AnswerMapper;
import com.yonyou.cyxdms.basedata.service.vo.questionaire.AnswerGroupVO;
import com.yonyou.cyxdms.basedata.service.vo.questionaire.AnswerVO;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.framework.constants.FrameworkCommonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 答案组 服务实现类
 * </p>
 *
 * @author renwd
 * @since 2019-09-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AnswerGroupServiceImpl implements AnswerGroupService {

    @Autowired
    private AnswerGroupMapper answerGroupMapper;

    @Autowired
    private AnswerMapper answerMapper;


    /**
     * 答案信息查询
     *
     * @param page page
     * @param queryAnswerDTO queryAnswerDTO
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.questionaire.AnswerGroupVO>
     * @author renwd
     * @since 2019/9/19
     */
    @Override
    public IPage<AnswerGroupVO> queryAnswerGroupInfo(Page<AnswerGroupVO> page, QueryAnswerDTO queryAnswerDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        queryAnswerDTO.setAppId(loginInfoDto.getAppId());
        queryAnswerDTO.setOwnerCode(loginInfoDto.getOwnerCode());
        page.setRecords(answerGroupMapper.listAnswerGroupInfo(page, queryAnswerDTO));
        return page;
    }

    /**
     * 根据答案组id查询答案信息
     *
     * @param answerGroupId
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.questionaire.AnswerVO>
     * @author renwd
     * @since 2019/9/19
     */
    @Override
    public IPage<AnswerVO> queryAnswerInfoById(Page<AnswerVO> page, Long answerGroupId) {
        page.setRecords(answerGroupMapper.listAnswerByGroupId(page, answerGroupId));
        return page;
    }

    /**
     * 根据答案组id查询答案信息(不分页)
     *
     * @param answerGroupId
     * @return java.util.List<com.yonyou.cyxdms.basedata.service.vo.questionaire.AnswerVO>
     * @author renwd
     * @since 2019/9/24
     */
    @Override
    public List<AnswerVO> listAnswerInfoById(Long answerGroupId) {
        QueryWrapper<AnswerPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AnswerPO::getAnswerGroupId, answerGroupId);
        List<AnswerPO> answerPoList = answerMapper.selectList(queryWrapper);
        return BeanMapperUtil.copyList(answerPoList, AnswerVO.class);
    }

    /**
     * 根据答案组id查询答案组信息
     *
     * @param answerGroupId
     * @return com.yonyou.cyxdms.basedata.service.vo.questionaire.AnswerGroupVO
     * @author renwd
     * @since 2019/9/19
     */
    @Override
    public AnswerGroupVO queryAnswerGroupById(Long answerGroupId) {
        AnswerGroupPO answerGroupPo = answerGroupMapper.selectById(answerGroupId);
        AnswerGroupVO answerGroupVo = BeanMapperUtil.copyProperties(answerGroupPo, AnswerGroupVO.class);
        return answerGroupVo;
    }

    /**
     * 新增答案
     *
     * @param answerGroupDTO answerGroupDTO
     * @return java.lang.Integer
     * @author renwd
     * @since 2019/9/19
     */
    @Override
    public Integer insertAnswerInfo(AnswerGroupDTO answerGroupDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        QueryWrapper<AnswerGroupPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AnswerGroupPO::getAppId, loginInfoDto.getAppId())
                .eq(AnswerGroupPO::getOwnerCode, loginInfoDto.getOwnerCode())
                .eq(AnswerGroupPO::getAnswerGroupName, answerGroupDTO.getAnswerGroupName());
        List<AnswerGroupPO> answerGroupPoList = answerGroupMapper.selectList(queryWrapper);
        if (!CommonUtils.isNullOrEmpty(answerGroupPoList)) {
            throw new ServiceBizException("该答案已经存在");
        }

        // 新增答案组
        AnswerGroupPO answerGroupPo = new AnswerGroupPO();
        answerGroupPo.setAnswerGroupName(answerGroupDTO.getAnswerGroupName());
        answerGroupPo.setAnswerGroupDescript(answerGroupDTO.getAnswerGroupDescript());
        answerGroupPo.setIsValid(answerGroupDTO.getIsValid());
        answerGroupPo.setDataSources(answerGroupDTO.getDataSources());
        answerGroupMapper.insert(answerGroupPo);

        // 保存答案
        insertAnswer(answerGroupDTO, loginInfoDto, answerGroupPo);
        return 1;
    }

    /**
     * 修改答案
     *
     * @param answerGroupDTO answerGroupDTO
     * @return java.lang.Integer
     * @author renwd
     * @since 2019/9/19
     */
    @Override
    public Integer updateAnswerInfo(AnswerGroupDTO answerGroupDTO) {
        // 修改答案组
        AnswerGroupPO answerGroupPo = answerGroupMapper.selectById(answerGroupDTO.getAnswerGroupId());
        answerGroupPo.setAnswerGroupName(answerGroupDTO.getAnswerGroupName());
        answerGroupPo.setAnswerGroupDescript(answerGroupDTO.getAnswerGroupDescript());
        answerGroupPo.setIsValid(answerGroupDTO.getIsValid());
        answerGroupPo.setRecordVersion(answerGroupDTO.getRecordVersion());
        int updateNum = answerGroupMapper.updateById(answerGroupPo);
        if (updateNum == 0) {
            throw new ServiceBizException(FrameworkCommonConstants.UPDATE_FAILED);
        }

        // 删除答案
        QueryWrapper<AnswerPO> deleteAnswerGroupWrapper = new QueryWrapper<>();
        deleteAnswerGroupWrapper.lambda().eq(AnswerPO::getAnswerGroupId, answerGroupDTO.getAnswerGroupId());
        answerMapper.delete(deleteAnswerGroupWrapper);

        // 保存答案
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        insertAnswer(answerGroupDTO, loginInfoDto, answerGroupPo);
        return 1;
    }

    /**
     * 删除答案组及答案
     *
     * @param answerGroupId
     * @return java.lang.Integer
     * @author renwd
     * @since 2019/9/19
     */
    @Override
    public Integer deleteAnswerGroupInfo(Long answerGroupId) {
        QueryWrapper<AnswerPO> deleteAnswerWrapper = new QueryWrapper<>();
        deleteAnswerWrapper.lambda().eq(AnswerPO::getAnswerGroupId, answerGroupId);
        answerMapper.delete(deleteAnswerWrapper);

        return answerGroupMapper.deleteById(answerGroupId);
    }


    /**
     * 新增答案
     *
     * @param answerGroupDTO answerGroupDTO
     * @param loginInfoDto   loginInfoDto
     * @param answerGroupPo  answerGroupPO
     * @author renwd
     * @since 2019/9/19
     */
    private void insertAnswer(AnswerGroupDTO answerGroupDTO, LoginInfoDto loginInfoDto, AnswerGroupPO answerGroupPo) {
        List<AnswerDTO> answerGroupList = answerGroupDTO.getAnswerGroupList();
        if (!CommonUtils.isNullOrEmpty(answerGroupList)) {
            for (AnswerDTO answerDTO : answerGroupList) {
                QueryWrapper<AnswerPO> answerQueryWrapper = new QueryWrapper<>();
                answerQueryWrapper.lambda().eq(AnswerPO::getAppId, loginInfoDto.getAppId())
                        .eq(AnswerPO::getOwnerCode, loginInfoDto.getOwnerCode())
                        .eq(AnswerPO::getAnswerGroupId, answerGroupPo.getAnswerGroupId())
                        .eq(AnswerPO::getAnswer,answerDTO.getAnswer());
                List<AnswerPO> answerPoList = answerMapper.selectList(answerQueryWrapper);
                if (CommonUtils.isNullOrEmpty(answerPoList)) {
                    AnswerPO answerPo = new AnswerPO();
                    answerPo.setAnswerGroupId(answerGroupPo.getAnswerGroupId());
                    answerPo.setAnswer(answerDTO.getAnswer());
                    answerPo.setAnswerSort(answerDTO.getAnswerSort());
                    answerMapper.insert(answerPo);
                }
            }
        }
    }



}
